
<div class="gid_6_table_title" style="display:block">数据字典</div>
<div class="gid_6_table_content">
    <div class="layui-card gid_6">
        <!-- 搜索表单区 -->
        <form class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="gid-6-tno-2-sys_dic-search-form">
            <div class="layui-form-item">
            #set(initValue = 'dic_pid' == filterKey?filterValue:'')
            #set(disabled = 'dic_pid'.equals(filterKey))

            <!-- 上级编号组件类型：select -->
                        <!-- 下拉表单 -->
                        <div class="layui-inline">
                            <label class="layui-form-label">上级编号</label>
                            <div class="layui-input-inline">
                                <select id="gid-6-tno-2_dic_pid_search_select" name="dic_pid" xm-select-search="" xm-select-radio=""
                                    placeholder="请选择上级编号" autocomplete="off" xm-select-height="36px" xm-select-skin="normal" xm-select="gid-6-tno-2_dic_pid_search_select" lay-filter="gid-6-tno-2_dic_pid_search_select">
                                    <option value="">请选择上级编号</option>
                                </select>
                            </div>
                        </div>

            #set(initValue = 'dic_code' == filterKey?filterValue:'')
            #set(disabled = 'dic_code'.equals(filterKey))

            <!-- 字典代码组件类型：text -->
                    <div class="layui-inline">
                        <label class="layui-form-label">字典代码</label>
                        <div class="layui-input-inline">
                            <input type="text" name="dic_code" value="#(initValue)" #(disabled?'disabled':'') placeholder="请输入字典代码" class="layui-input" autocomplete="off">
                        </div>
                    </div>

            #set(initValue = 'dic_name' == filterKey?filterValue:'')
            #set(disabled = 'dic_name'.equals(filterKey))

            <!-- 字典名称组件类型：text -->
                    <div class="layui-inline">
                        <label class="layui-form-label">字典名称</label>
                        <div class="layui-input-inline">
                            <input type="text" name="dic_name" value="#(initValue)" #(disabled?'disabled':'') placeholder="请输入字典名称" class="layui-input" autocomplete="off">
                        </div>
                    </div>

            #set(initValue = 'dic_value' == filterKey?filterValue:'')
            #set(disabled = 'dic_value'.equals(filterKey))

            <!-- 字典值组件类型：text -->
                    <div class="layui-inline">
                        <label class="layui-form-label">字典值</label>
                        <div class="layui-input-inline">
                            <input type="text" name="dic_value" value="#(initValue)" #(disabled?'disabled':'') placeholder="请输入字典值" class="layui-input" autocomplete="off">
                        </div>
                    </div>

            #set(initValue = 'is_enable' == filterKey?filterValue:'')
            #set(disabled = 'is_enable'.equals(filterKey))

            <!-- 是否启用组件类型：select -->
                                    <!-- 布尔表单 -->
                                    <div class="layui-inline">
                                        <label class="layui-form-label">是否启用</label>
                                        <div class="layui-input-inline">
                                            <select name="is_enable" #(disabled?'disabled':'') lay-verify="">
                                            <option value="">请选择</option>
                                            <option value="1" #(initValue == true?'selected':'')>是</option>
                                            <option value="0" #(initValue == false?'selected':'')>否</option>
                                            </select>
                                        </div>
                                    </div>

                <div class="layui-inline">
                    <button class="layui-btn" lay-submit lay-filter="gid-6-tno-2-btn-sys_dic-form-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
        <!-- 列表区 -->
        <div class="layui-card-body">

            <!-- 头部工具栏模板 -->
            <script type="text/html" id="sys_dic_header_toolbar">
                <div class="layui-btn-container">
                    <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
                    <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="dels">删除</button>
                </div>
            </script>

            <!-- 列表表格 -->
            <table id="gid-6-tno-2-sys_dic-table" lay-filter="gid-6-tno-2-sys_dic-table" style="width: 100%"></table>

            <!-- 是否启用列表显示模板 -->
            <script type="text/html" id="gid-6-tno-2-is_enable_templet">
                <b style="color:{{ d.is_enable?'green':'red' }};">{{ d.is_enable ?'是':'否' }}</b>
            </script>

            <!-- 每行操作按钮模板 -->
            <script type="text/html" id="gid-6-tno-2-sys_dic_row_toolbar">
                <a class="layui-btn layui-btn-default layui-btn-xs" lay-event="edit" title="编辑"><i class="layui-icon layui-icon-edit table_btn_icon"></i></a>
                <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" title="删除"><i class="layui-icon layui-icon-delete table_btn_icon"></i></a>
            </script>
        </div>
    </div>
</div>
<!-- JS脚本区 -->
<script>

    layui.use(['table', 'form', 'layer', 'laydate', 'jquery', 'admin', 'number', 'xSelects'], function () {
        var form = layui.form, layer = layui.layer, laydate = layui.laydate, table = layui.table, admin = layui.admin, number = layui.number, xSelects = layui.xSelects;

        form.render(null, 'gid-6-tno-2-sys_dic-search-form');

                    var initValue = '#('dic_pid' == filterKey?filterValue:'')';
                    #set(disabled = 'dic_pid'.equals(filterKey))
                    //渲染上级编号下拉分页选择器
                    xSelects.render({
                        selectId: 'gid-6-tno-2_dic_pid_search_select',
                        url: '/system/dic/table2/dic_pid/sql/list',
                        titleKey: 'dic_name',
                        valueKey: 'dic_id',
                        searchKey: 'dic_name__op__like',
                        showPage: true,
                        size: 10,
                        multiSelect: false,
                        model: { type: 'fixed' },
                        zIndex: 1000000000,
                        disabled: #(disabled),
                        init: initValue!=''?[{val:initValue}]:[],
                        // 初始值设置
                        initValues: initValue!=''?[initValue]:[]
                    });

        // 表单搜索监听
        form.on('submit(gid-6-tno-2-btn-sys_dic-form-search)', function (data) {
            console.log('gid-6-tno-2-sys_dic-search-form', data)
            var field = data.field;
            #if(notBlank(filterKey))
            field = { ...field, '#(filterKey)': '#(filterValue)' }
            #end
            //执行重载
            table.reload('gid-6-tno-2-sys_dic-table', {
                where: field,
                page: {
                    curr: 1 //重新从第 1 页开始
                }
            });
            return false;
        });
        // 渲染表格数据
        var tableIns = table.render({
            id: 'gid-6-tno-2-sys_dic-table',
            elem: '#gid-6-tno-2-sys_dic-table',
            // 树表格专用参数
            tree: {
                iconIndex: 1,
                isPidData: true,
                idName: 'dic_id',
                pidName: '',
                arrowType: 'arrow2',
                onlyIconControl: true,
            },
            toolbar: '#sys_dic_header_toolbar',
            url: '/system/dic/table2/list',
            #if(notBlank(filterKey))
            where: { #(filterKey): "#(filterValue)" },
            #end
            loading: true,
            page: true, //开启分页
            cols: [[
                {type: 'checkbox', fixed: 'left'},

                {field: 'dic_pid', title: '上级编号', templet: function (row){
                    if(row.dic_pid_title){
                        return row.dic_pid_title;
                    }
                    return '--';
                }},

                    {field: 'dic_code', title: '字典代码'},

                    {field: 'dic_name', title: '字典名称'},

                    {field: 'dic_value', title: '字典值'},

                    {field: 'create_datetime', title: '创建时间'},

                    {field: 'is_enable', title: '是否启用', event: 'is_enable', templet: "#gid-6-tno-2-is_enable_templet"},
                              {title: '操作', width: '', fixed: 'right', templet: '#gid-6-tno-2-sys_dic_row_toolbar'}
            ]],
            done: function (a,b,c) {
                console.log('tableIns', tableIns);
                tableIns.expandAll&&tableIns.expandAll()
            }
        });

        table['gid-6-tno-2-sys_dic-table'] = tableIns;

        // 监听表格工具栏事件
        table.on("toolbar(gid-6-tno-2-sys_dic-table)", eventListener)

        // 监听表格行事件
        table.on("tool(gid-6-tno-2-sys_dic-table)", eventListener)

                        ;
        // 事件监听
        function eventListener(obj) {
            let event = obj.event;
            let data = obj.data;
            var postData = {};
            #if(notBlank(filterKey))
            postData.filterKey = '#(filterKey)';
            postData.filterValue = '#(filterValue)';
            #end
            switch (event) {
                case 'addSub':
                    postData.filterKey = '';
                    postData.filterValue = data.dic_id;
                case 'add':
                    layui.$.post('/system/dic/table2/editPage', postData, function (str) {
                        layer.open({
                            type: 1,
                            title: '新建数据字典',
                            maxmin: true,
                            area: ['800px', '535px'],
                            content: str,
                        })
                    });
                    break;
                case 'edit':
                    postData.dic_id = data.dic_id;
                    layui.$.post('/system/dic/table2/editPage', postData, function (str) {
                        layer.open({
                            type: 1,
                            title: '编辑数据字典',
                            maxmin: true,
                            area: ['800px', '535px'],
                            content: str,
                        })
                    });
                    break;
                case 'dels':
                    let checkStatus = table.checkStatus('gid-6-tno-2-sys_dic-table');
                    let selectRow = checkStatus.data;
                    deleteRow(selectRow);
                    break;
                case 'del':
                    let row = obj.data;
                    deleteRow([row]);
                    break;
            }
            console.log('toolbar_event', obj)
        }

        // 删除行方法
        function deleteRow(selectRows) {
            if (selectRows.length <= 0) {
                layer.msg('请先钩选要删除的记录行！');
                return;
            }
            layer.confirm('确定要删除这' + selectRows.length + '条记录吗？',
                {icon: 3, title: '提示', btn: ['确认删除', '取消']},
                function (index) {
                    console.log(index);
                    layui.$.ajax({
                        url: '/system/dic/table2/delete',
                        data: {
                            ids: selectRows.map((row) => row.dic_id)
                        },
                        traditional: true,
                        type: 'POST',
                        success: function (result) {
                            if (result.success) {
                                layer.msg('删除成功', {icon: 6});
                                table.reload('gid-6-tno-2-sys_dic-table');
                            } else {
                                layer.msg('删除失败！' + result.message, {icon: 5});
                            }
                        }
                    });
                });
        }
    })
</script>